package com.lollipop.ucenter.shiro;

import java.util.Set;

import com.lollipop.ucenter.rpc.api.UserService;
import com.lollipop.ucenter.rpc.model.User;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

@Component
@Lazy(false)
public class UserUtils {

    @Autowired
    private UserService userService;
    private static UserUtils userUtils;

    public UserUtils() {
        userUtils = this;
    }

    public static CustomRealm.ShiroUser getShiroUser() {
        return (CustomRealm.ShiroUser) SecurityUtils.getSubject().getPrincipal();
    }

    public static User getCurrentUser() {
        return userUtils.userService.findByUsername(getShiroUser().getName());
    }

    public static Set<String> getUserPermission() {
        return userUtils.userService.findUserPermissions(getShiroUser().getId());
    }
}
